package com.hziee.mail.dao;

import com.hziee.mail.model.AddressInfoEntity;
import com.hziee.mail.model.OrderInfoEntity;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;
import java.util.Map;

/**
 * @author ZX
 */
public interface OrderInfoRepository extends JpaRepository<OrderInfoEntity, Integer>, CrudRepository<OrderInfoEntity, Integer> {


    /**
     * 通过userId查询订单信息
     *
     * @param userId
     * @return
     */
    @Query(nativeQuery = true, value = "SELECT " +
            "order_info.order_id, " +
            "order_info.order_number, " +
            "order_info.order_time, " +
            "order_info.goods_id, " +
            "order_info.user_id, " +
            "order_info.price, " +
            "order_info.value, " +
            "order_info.allmoney, " +
            "order_info.order_status, " +
            "order_info.linkman, " +
            "order_info.telephone, " +
            "order_info.address, " +
            "order_info.pya_date, " +
            "order_info.send_date, " +
            "order_info.end_date, " +
            "order_info.ly, " +
            "order_info.pay_style, " +
            "product_image_info.p_image_path, " +
            "product_class_info.product_class_name " +
            "FROM " +
            "order_info " +
            "JOIN product_image_info on product_image_info.product_class_id = order_info.goods_id " +
            "JOIN product_class_info on product_class_info.product_class_id = order_info.goods_id " +
            "WHERE order_info.user_id = :userId")
    List<Map> getOrderInfo(@Param("userId") Integer userId);


    /**
     * 查看订单状态
     *
     * @param userId
     * @param type
     * @return
     */
    @Query(nativeQuery = true, value = "SELECT " +
            "order_info.order_id, " +
            "order_info.order_number, " +
            "order_info.order_time, " +
            "order_info.goods_id, " +
            "order_info.user_id, " +
            "order_info.price, " +
            "order_info.value, " +
            "order_info.allmoney, " +
            "order_info.order_status, " +
            "order_info.linkman, " +
            "order_info.telephone, " +
            "order_info.address, " +
            "order_info.pya_date, " +
            "order_info.send_date, " +
            "order_info.end_date, " +
            "order_info.ly, " +
            "order_info.pay_style, " +
            "product_image_info.p_image_path, " +
            "product_class_info.product_class_name " +
            "FROM " +
            "order_info " +
            "JOIN product_image_info on product_image_info.product_class_id = order_info.goods_id " +
            "JOIN product_class_info on product_class_info.product_class_id = order_info.goods_id " +
            "WHERE order_info.user_id = :userId and order_info.order_status = :type")
    List<Map> getOrderInfoType(@Param("userId") Integer userId, @Param("type") Integer type);

    /**
     * 通过order_number查询订单信息
     *
     * @param order_number
     * @return
     */
    @Query(nativeQuery = true, value = "SELECT " +
            "order_info.order_id, " +
            "order_info.order_number, " +
            "order_info.order_time, " +
            "order_info.goods_id, " +
            "order_info.user_id, " +
            "order_info.price, " +
            "order_info.value, " +
            "order_info.allmoney, " +
            "order_info.order_status, " +
            "order_info.linkman, " +
            "order_info.telephone, " +
            "order_info.address, " +
            "order_info.pya_date, " +
            "order_info.send_date, " +
            "order_info.end_date, " +
            "order_info.ly, " +
            "order_info.pay_style, " +
            "product_image_info.p_image_path, " +
            "product_class_info.product_class_name " +
            "FROM " +
            "order_info " +
            "JOIN product_image_info on product_image_info.product_class_id = order_info.goods_id " +
            "JOIN product_class_info on product_class_info.product_class_id = order_info.goods_id " +
            "WHERE order_info.order_number = :order_number")
    List<Map> getOrderDetails(@Param("order_number") String order_number);
}
